package com.entrogy.config;

import com.alibaba.fastjson.JSONObject;

import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.HttpResponse;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

/**
 * @Author 许红松
 * @date 2019/5/6 0006 上午 11:44
 */
public class HttpClientUtils {

    /**
     *
     * @param requestUrl
     * @param json
     * @return
     */
    public static String doPost(String requestUrl,JSONObject json){
        //HttpClient client = new DefaultHttpClient();
        HttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(requestUrl);
        post.setHeader("Content-Type", "application/json");
        post.addHeader("Authorization", "Basic YWRtaW46");
        String result = "";
        try {
            StringEntity s = new StringEntity(json.toString(), "utf-8");
            s.setContentEncoding(new BasicHeader("contentType",
                    "application/json"));
            post.setEntity(s);
            // 发送请求
            HttpResponse httpResponse = client.execute(post);
            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
                strber.append(line + "\n");
            inStream.close();
            result = strber.toString();
            System.out.println(result);
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                System.out.println("请求服务器成功，做相应处理");

            } else {
                System.out.println("请求服务端失败");
            }
        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        }
        return result;
    }

    /**
     *
     * @param requestUrl
     * @param json
     * @return
     */
    public static String doGet(String requestUrl,JSONObject json) {
        InputStream in = null;
        String result = "";
        try {
            //HttpClient httpClient = new DefaultHttpClient();// 实例化HttpClient类
            //HttpClient client = new DefaultHttpClient();
            HttpClient client = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(requestUrl);// 用get方法请求
            HttpResponse response = client.execute(httpGet);// 执行get请求方法返回
            // response

            int statusCode = response.getStatusLine().getStatusCode();// 通过获取状态行再获取状态码
            /* Log.v("", "statusCode>>>>>:" + statusCode); */

            if (statusCode == HttpURLConnection.HTTP_OK) {// statusCode==200isok

                in = response.getEntity().getContent();// 获取信息内容

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(in, "UTF-8"));
                StringBuilder sb = new StringBuilder();
                String line = null;

                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                result = sb.toString();
                /* Log.v("", "sb.toString():" + sb.toString()); */
                return sb.toString();
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;

    }


}
